function loadEmployeeList(deptId, typeId, mapped) {	
	$.ajax ({
		type:	'GET',
		url:	'../HumanResources/getEmployeeList?set_ajax_view',
		data:	'deptId=' + deptId + 
				'&typeId=' + typeId + 
				'&mapped=' + mapped,		
		success: function(data) {
			$('#employeeListResult').html(data);
			$('#employeeList tr:last').addClass('last');
		}
	});
}

function loadPage(sourceDiv, actionURL, personId) {
	hideStatusMessage();
	$.ajax ({
		type:	'GET',
		url:	actionURL + '?set_ajax_view',
		data:	'employeeId=' + personId,			
		success: function(data) {
			$("#" + sourceDiv).html(data);
		}
	});	
}

function editEmployee() {
	if (!checkMonth($('#dob_mm'), false, '', 'status_message')) {
		$('#status_message').show();
		return false;
	} 
	
	if (!checkDay($('#dob_dd'),false,'','status_message')) {
		$('#status_message').show();	
		return false;
	}	
	var res = checkDate($('#dob_yyyy'), $('#dob_mm'), $('#dob_dd'), false, '', 'status_message', true);
	if(!res){
		$('#status_message').show();
		return false;
	}
	
	var date = $.trim($('#dob_dd').val());
	var month = $.trim($('#dob_mm').val());
	var year = $.trim($('#dob_yyyy').val());
	if (date.length == 0) {
		date = 1;
	} 
	if (month.length == 0) {
		month = 1;
	}		
	if (!validateForm()) {
		return;
	}
	
	$.ajax({
		type:	"POST",
		url:	"../HumanResources/editEmployeeAjax?set_ajax_view",		
		data:	$('#employeeForm').serialize(),				
		beforeSend: function() {
			hideStatusMessage();
		},
		success:	function(data) {
			var dataValue = parseInt(data);
			if (dataValue == -1) {
				showCustomDialog(_('Error'), 
						_('Unexpected error, please try again'), 'error');
			} else {
				showCustomDialog(_('Update employee'), 
						_('Update successfully'), 'ok');
				loadPage('managementContent', '../HumanResources/EditEmployee', dataValue);				
			}			
		} 																
	});		
}

function showStatusMessage(message) {
	$('#status_message').text(message).css('color', 'green').show();	
}

function hideStatusMessage(){	
	$('#status_message').text('').hide();
}

function validateForm() {
	var message = "";
	if(!isNotEmpty($('#name'),'','status_message')) {
		return displayError(_("Empty name"));
	}
	
	if( $("input:radio[name='employee[gender]']:checked").length == 0) {
		return displayError(_("Not yet select gender"));
	}
	return true;				
}

function displayError(message) {
	$('#status_message').text(message).css('color', 'red').show();	
	return false;
}

function showCustomDialog(title, message, iconType) {
	var buttons = {};
	buttons[_('Close')] = function() {
		$(this).dialog('close');
	};
	
	generalDialog(null, title, message, iconType, {
		autoOpen: true,			
		bgiframe: true,
		resizable: false,
		modal: true,
		buttons: buttons,
		dialogClass: 'custom-dialog'
	}, _('Close'));
}